/**
 * 
 */
package com.wutianyi.cipher;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;


/**
 * @author wutianyi
 *
 */
public class DesEncrypt {
	Key key;
	
	public void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			_generator.init(new SecureRandom(strKey.getBytes()));
			
			this.key = _generator.generateKey();
			_generator = null;
//			DESKeySpec keySpec = new DESKeySpec(strKey.getBytes());
//			key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
		}catch(NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getEncString(String strMing) {
		return byte2Hex(getEncCode(strMing.getBytes()));
		
	}
	
	public String getDesString(String strMi) {
		return new String(getDesCode(hex2Byte(strMi.getBytes())));
	}
	
	private byte[] getEncCode(byte[] bytes) {
		byte[] byteFina = null;
		Cipher cipher = null;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byteFina = cipher.doFinal(bytes);
		}catch(NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return byteFina;
	}
	
	private byte[] getDesCode(byte[] byteD) {
		Cipher cipher = null;
		byte[] byteFina = null;
		
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byteFina = cipher.doFinal(byteD);
		}catch(NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return byteFina;
	}
	
	public static String byte2Hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for(int n = 0; n < b.length; n ++) {
			stmp = Integer.toHexString(b[n] & 0XFF);
			if(stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}
	
	public static byte[] hex2Byte(byte[] b) {
		if(b.length % 2 != 0) {
			throw new IllegalArgumentException();
		}
		byte[]b2 = new byte[b.length / 2];
		for(int n = 0; n < b.length; n += 2) {
			String item = new String(b, n, 2);
			b2[n/2] = (byte)Integer.parseInt(item, 16);
		}
		return b2;
	}
	 
	public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {

		DesEncrypt encrypt = new DesEncrypt();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss|");
		System.out.println(dateFormat.format(new Date()));
		encrypt.getKey("ku.duowan.com");
		//DA646345D073C7DD99B261D063BA83BA
		String mi = encrypt.getEncString(dateFormat.format(new Date()) + "wutianyi");
		String ming = encrypt.getDesString(mi);
		System.out.println(encrypt.getEncString("wutianyi"));
		System.out.println(ming);
		System.out.println(Integer.toHexString(0 & 0xff));
		System.out.println((byte)Integer.parseInt(Integer.toHexString(-1 & 0xff), 16));
		System.out.println(Integer.toHexString(-1 & 0xff));
		
		String url = "http://ku.duowan.com/moba/?yxtq_ka_content";
		String u = "http://www.baidu.com?key=" + mi + "&redirect=" + URLEncoder.encode(url);
		System.out.println(u);
	}
}	
